package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.model.BackCost;
import com.feeyin.radar.crm.model.Contract;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.Linkman;
import com.feeyin.radar.crm.model.ManageReport;
import com.feeyin.radar.crm.model.MarketReport;
import com.feeyin.radar.crm.model.MarketTotal;
import com.feeyin.radar.crm.model.ReportForMarket2;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.ManageReportService;
import com.feeyin.radar.sys.service.DepartmentService;
import java.io.BufferedOutputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ManageReportController extends MultiActionController
{
  private DepartmentService departmentService;
  private ManageReportService manageReportService;
  private CustomerService customerService;
  private String manageReport;
  private String salePeriodReport;
  private String selectDept;
  private String customerContractReport;
  private String saleContractReport;
  private String saleMonthPeriodReport;
  private String getUserContract;
  private String oldCustomerDevoteAnalysis;
  private String allCustomerDevoteAnalysis;
  private String linkManMailInfo;
  private String marketExpandsAnalysis1;
  private String annualWreathCompares;

  public String getAnnualWreathCompares()
  {
    return this.annualWreathCompares;
  }

  public void setAnnualWreathCompares(String annualWreathCompares) {
    this.annualWreathCompares = annualWreathCompares;
  }
  public String getMarketExpandsAnalysis1() {
    return this.marketExpandsAnalysis1;
  }

  public void setMarketExpandsAnalysis1(String marketExpandsAnalysis) {
    this.marketExpandsAnalysis1 = marketExpandsAnalysis;
  }
  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }
  public String getLinkManMailInfo() {
    return this.linkManMailInfo;
  }

  public void setLinkManMailInfo(String linkManMailInfo) {
    this.linkManMailInfo = linkManMailInfo;
  }

  public String getGetUserContract() {
    return this.getUserContract;
  }

  public void setGetUserContract(String getUserContract) {
    this.getUserContract = getUserContract;
  }
  public String getAllCustomerDevoteAnalysis() {
    return this.allCustomerDevoteAnalysis;
  }

  public void setAllCustomerDevoteAnalysis(String allCustomerDevoteAnalysis) {
    this.allCustomerDevoteAnalysis = allCustomerDevoteAnalysis;
  }

  public String getOldCustomerDevoteAnalysis() {
    return this.oldCustomerDevoteAnalysis;
  }

  public void setOldCustomerDevoteAnalysis(String oldCustomerDevoteAnalysis) {
    this.oldCustomerDevoteAnalysis = oldCustomerDevoteAnalysis;
  }

  public String getCustomerContractReport() {
    return this.customerContractReport;
  }

  public void setCustomerContractReport(String customerContractReport) {
    this.customerContractReport = customerContractReport;
  }

  public DepartmentService getDepartmentService() {
    return this.departmentService;
  }

  public void setDepartmentService(DepartmentService departmentService) {
    this.departmentService = departmentService;
  }

  public ManageReportService getManageReportService() {
    return this.manageReportService;
  }

  public void setManageReportService(ManageReportService manageReportService) {
    this.manageReportService = manageReportService;
  }

  public String getManageReport() {
    return this.manageReport;
  }

  public void setManageReport(String manageReport) {
    this.manageReport = manageReport;
  }

  public String getSalePeriodReport() {
    return this.salePeriodReport;
  }

  public void setSalePeriodReport(String salePeriodReport) {
    this.salePeriodReport = salePeriodReport;
  }

  public String getSelectDept() {
    return this.selectDept;
  }

  public void setSelectDept(String selectDept) {
    this.selectDept = selectDept;
  }

  public String getSaleContractReport() {
    return this.saleContractReport;
  }

  public void setSaleContractReport(String saleContractReport) {
    this.saleContractReport = saleContractReport;
  }

  public String getSaleMonthPeriodReport() {
    return this.saleMonthPeriodReport;
  }

  public void setSaleMonthPeriodReport(String saleMonthPeriodReport) {
    this.saleMonthPeriodReport = saleMonthPeriodReport;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    return new ModelAndView(getManageReport());
  }

  public ModelAndView selectDept(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    if (!StringUtils.isEmpty(isViewAllManageReport))
      model.put("departments", this.departmentService.getDepartments());
    else {
      model.put("departments", this.departmentService.getCurrentDeptAndSubDeptsByUserId(userId));
    }

    return new ModelAndView(getSelectDept(), model);
  }

  public ModelAndView preSalePeriodReport(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    ManageReport mr = new ManageReport();
    mr.setStartDate(DateUtils.getCurrentSeasonFirstDay());
    mr.setEndDate(DateUtils.getCurrentSeasonEndDay());
    mr.setDeptIds(this.departmentService.getCurrentDeptIdAndSubDeptIdsByUserId(userId));
    mr.setDeptNames(this.departmentService.getCurrentDeptNameAndSubDeptNamesByUserId(userId));
    mr.setUserId(userId);

    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    String sign = "";
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) || (!StringUtils.isEmpty(isViewAllManageReport))) {
      sign = "1";
    }
    model.put("manageReport", mr);
    model.put("salePeriodReport", this.manageReportService.getBusinessFinishStatus(sign, mr));
    model.put("salePeriodReport4", this.manageReportService.getPeriodNewSaleCustomer(sign, mr));
    return new ModelAndView(getSalePeriodReport(), model);
  }

  public ModelAndView salePeriodReport(HttpServletRequest request, HttpServletResponse response, ManageReport manageReport)
  {
    Map model = new HashMap();
    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    String sign = "";
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) || (!StringUtils.isEmpty(isViewAllManageReport))) {
      sign = "1";
    }
    model.put("manageReport", manageReport);
    model.put("salePeriodReport", this.manageReportService.getBusinessFinishStatus(sign, manageReport));
    model.put("salePeriodReport4", this.manageReportService.getPeriodNewSaleCustomer(sign, manageReport));
    return new ModelAndView(getSalePeriodReport(), model);
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
  {
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());
  }

  public ModelAndView CustomerStatisticContract(HttpServletRequest request, HttpServletResponse response) {
    Integer userid = (Integer)request.getSession().getAttribute("userId");
    Map model = new HashMap();
    Contract contract = new Contract();
    Page page = new Page();

    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    if ((!StringUtils.isEmpty(isViewDeptManageReport)) && (StringUtils.isEmpty(isViewAllManageReport))) {
      String deptids = this.departmentService.getCurrentDeptIdAndSubDeptIdsByUserId(userid);
      contract.setDeptIds(deptids);
      page.getPageList(this.manageReportService.getCountForConstractByCustomerForUser(contract).intValue(), null, request);
      contract.setPageStartRow(page.getPageStartRow());
      contract.setPageEndRow(page.getPageEndRow());
      model.put("report", this.manageReportService.getConstractByCustomerForUser(contract));
      return new ModelAndView(getCustomerContractReport(), model);
    }
    if (!StringUtils.isEmpty(isViewAllManageReport)) {
      page.getPageList(this.manageReportService.getCountForConstractByCustomerForUser(contract).intValue(), null, request);
      contract.setPageStartRow(page.getPageStartRow());
      contract.setPageEndRow(page.getPageEndRow());
      model.put("report", this.manageReportService.getConstractByCustomerForUser(contract));
      return new ModelAndView(getCustomerContractReport(), model);
    }

    contract.setSalePersonId(userid);
    page.getPageList(this.manageReportService.getCountForConstractByCustomerForUser(contract).intValue(), null, request);
    contract.setPageStartRow(page.getPageStartRow());
    contract.setPageEndRow(page.getPageEndRow());
    model.put("report", this.manageReportService.getConstractByCustomerForUser(contract));
    return new ModelAndView(getCustomerContractReport(), model);
  }

  public ModelAndView saleStatisticContract(HttpServletRequest request, HttpServletResponse response) {
    Integer userid = (Integer)request.getSession().getAttribute("userId");
    Map model = new HashMap();
    Contract contract = new Contract();

    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    Page page = new Page();
    page.getPageList(this.manageReportService.gerCountForContractByUsers(contract, isViewDeptManageReport, isViewAllManageReport, userid).intValue(), null, request);
    model.put("report", this.manageReportService.getContractByUsers(contract, isViewDeptManageReport, isViewAllManageReport, userid, page));
    return new ModelAndView(getSaleContractReport(), model);
  }

  public ModelAndView saleMonthReport(HttpServletRequest request, HttpServletResponse response, ManageReport manageReport) {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    Page page = new Page();

    ManageReport mr = new ManageReport();
    mr.setStartDate(DateUtils.getCurrentMonthFirstDay());
    mr.setEndDate(DateUtils.getCurrentMonthEndDay());
    mr.setDeptIds(this.departmentService.getCurrentDeptIdAndSubDeptIdsByUserId(userId));
    mr.setDeptNames(this.departmentService.getCurrentDeptNameAndSubDeptNamesByUserId(userId));
    mr.setUserId(userId);
    manageReport.setUserId(userId);
    Map model = new HashMap();
    if ((manageReport.getStartDate() == null) || (manageReport.getEndDate() == null))
      model = this.manageReportService.getBussinessFinishStatusForSale(isViewDeptManageReport, isViewAllManageReport, mr);
    else {
      model = this.manageReportService.getBussinessFinishStatusForSale(isViewDeptManageReport, isViewAllManageReport, manageReport);
    }
    return new ModelAndView(getSaleMonthPeriodReport(), model);
  }

  public ModelAndView getContractByUserid(HttpServletRequest request, HttpServletResponse response) {
    Integer userid = Integer.valueOf(Integer.parseInt(request.getParameter("id")));
    String sDate = request.getParameter("startDate");
    String eDate = request.getParameter("endDate");
    Date startDate = null;
    Date endDate = null;
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      startDate = sdf.parse(sDate);
      endDate = sdf.parse(eDate);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    BackCost backcost = new BackCost();
    backcost.setSalePersonId(userid);
    backcost.setStartDate(startDate);
    backcost.setEndDate(endDate);
    backcost.setGrossBackCostId(Integer.valueOf(2));

    Map model = new HashMap();

    model.put("Contract", this.manageReportService.getBackCostContractByDynamic(backcost));
    return new ModelAndView(getGetUserContract(), model);
  }

  public ModelAndView oldCustomerDevoteAnalysis(HttpServletRequest request, HttpServletResponse response, Customer customer) {
    List saleStages = this.manageReportService.getSaleStages();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    Map model = new HashMap();
    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    Page page = new Page();
    page.getPageList(this.manageReportService.getCountOldCustomerContractInfo(customer, isViewDeptManageReport, isViewAllManageReport, userId).intValue(), null, request);
    model.put("reports", this.manageReportService.getCustomerContractInfo(customer, isViewDeptManageReport, isViewAllManageReport, userId, page));
    model.put("records", this.customerService.getCustomerInfoRecords());
    model.put("ranges", this.customerService.getCustomerDictionaryRanges());
    model.put("customer", customer);
    model.put("saleStages", saleStages);
    return new ModelAndView(getOldCustomerDevoteAnalysis(), model);
  }

  public ModelAndView allCustomerDevoteAnalysis(HttpServletRequest request, HttpServletResponse response, Customer customer)
  {
    List saleStages = this.manageReportService.getSaleStages();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    Map model = new HashMap();
    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }

    Page page = new Page();
    page.getPageList(this.manageReportService.getCountAllCustomerInfoForContract(customer, isViewDeptManageReport, isViewAllManageReport, userId).intValue(), null, request);
    model.put("reports", this.manageReportService.getAllCustomerInfoForContract(customer, isViewDeptManageReport, isViewAllManageReport, userId, page));
    model.put("records", this.customerService.getCustomerInfoRecords());
    model.put("ranges", this.customerService.getCustomerDictionaryRanges());
    model.put("customer", customer);
    model.put("saleStages", saleStages);
    return new ModelAndView(getAllCustomerDevoteAnalysis(), model);
  }

  public ModelAndView getLinkManMailInfo(HttpServletRequest request, HttpServletResponse response, Customer customer) {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    Map model = new HashMap();
    Linkman linkman = new Linkman();
    linkman.setIndustryId(customer.getIndustryId());
    linkman.setAreaId(customer.getAreaId());
    linkman.setCompanyTypeId(customer.getCompanyTypeId());
    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    Page page = new Page();
    page.getPageList(this.manageReportService.getCountLinkManMailInfo(linkman, isViewDeptManageReport, isViewAllManageReport, userId).intValue(), null, request);
    model.put("records", this.customerService.getCustomerInfoRecords());
    model.put("ranges", this.customerService.getCustomerDictionaryRanges());
    model.put("customer", customer);
    model.put("report", this.manageReportService.getLinkManMailInfo(linkman, isViewDeptManageReport, isViewAllManageReport, userId, page));
    return new ModelAndView(getLinkManMailInfo(), model);
  }
  public ModelAndView outTxtForLinkManMail(HttpServletRequest request, HttpServletResponse response, Customer customer) {
    Linkman linkman = new Linkman();

    linkman.setIndustryId(customer.getIndustryId());
    linkman.setAreaId(customer.getAreaId());
    linkman.setCompanyTypeId(customer.getCompanyTypeId());
    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    Page page = new Page();

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    page.getPageList(this.manageReportService.getCountLinkManMailInfo(linkman, isViewDeptManageReport, isViewAllManageReport, userId).intValue(), Integer.valueOf(300), request);
    List mails = this.manageReportService.getLinkManMailInfo(linkman, isViewDeptManageReport, isViewAllManageReport, userId, page);
    System.out.print(mails.size());
    response.setContentType("text/plain");
    response.addHeader("Content-Disposition", 
      "attachment;filename=linkMan.txt");
    BufferedOutputStream buff = null;
    StringBuffer write = new StringBuffer();
    String tab = ",";
    String enter = "\r\n";
    ServletOutputStream outSTr = null;
    try {
      outSTr = response.getOutputStream();
      buff = new BufferedOutputStream(outSTr);
      for (int i = 0; i < mails.size(); i++) {
        linkman = (Linkman)mails.get(i);
        if ((linkman.getEmail() == null) || (linkman.getEmail().equals(""))) continue;
        write.append(linkman.getEmail())
          .append(tab);
      }

      buff.write(write.toString().getBytes("UTF-8"));
      buff.flush();
      buff.close();
    } catch (Exception e) {
      e.printStackTrace();
      try
      {
        buff.close();
        outSTr.close();
      } catch (Exception e1) {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        buff.close();
        outSTr.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return null;
  }

  public ModelAndView marketExpandsAnalysis(HttpServletRequest request, HttpServletResponse response) {
    Contract contract = new Contract();
    Contract contract1 = new Contract();
    Customer customer = new Customer();
    Customer customer1 = new Customer();
    Map model = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    String dateNow = DateUtils.getDateByFormat("yyyy-MM-dd");
    String currentYear = dateNow.substring(0, 4);
    Integer LastYear = Integer.valueOf(Integer.parseInt(currentYear) - 1);
    contract.setYears(currentYear);
    contract1.setYears(LastYear.toString());
    customer.setYears(currentYear);
    customer1.setYears(LastYear.toString());
    List All = new ArrayList();

    List lastc = this.manageReportService.getEveryMonthNewCustomerCount(customer1, isViewDeptManageReport, isViewAllManageReport, userId);
    List last = this.manageReportService.getEveryMonthNewContractCount(contract1, isViewDeptManageReport, isViewAllManageReport, userId);
    List thisc = this.manageReportService.getEveryMonthNewCustomerCount(customer, isViewDeptManageReport, isViewAllManageReport, userId);
    List this1 = this.manageReportService.getEveryMonthNewContractCount(contract, isViewDeptManageReport, isViewAllManageReport, userId);
    List totallastcustomer = this.manageReportService.getTotalYearNewCustomerCount(customer1, isViewDeptManageReport, isViewAllManageReport, userId);
    Customer totalcustomerL;
    if ((totallastcustomer.size() > 0) && (totallastcustomer != null))
      totalcustomerL = (Customer)totallastcustomer.get(0);
    else {
      totalcustomerL = new Customer();
    }
    List totalthiscustomer = this.manageReportService.getTotalYearNewCustomerCount(customer, isViewDeptManageReport, isViewAllManageReport, userId);
    Customer totalcustomerT;
    if ((totalthiscustomer.size() > 0) && (totalthiscustomer != null))
      totalcustomerT = (Customer)totalthiscustomer.get(0);
    else {
      totalcustomerT = new Customer();
    }
    List totallastcontract = this.manageReportService.getTotalYearNewContractCount(contract1, isViewDeptManageReport, isViewAllManageReport, userId);
    Contract totalcontractL;
    if ((totallastcontract.size() > 0) && (totallastcontract != null))
      totalcontractL = (Contract)totallastcontract.get(0);
    else {
      totalcontractL = new Contract();
    }
    List totalthiscontract = this.manageReportService.getTotalYearNewContractCount(contract, isViewDeptManageReport, isViewAllManageReport, userId);
    Contract totalcontractT;
    if ((totalthiscontract.size() > 0) && (totalthiscontract != null))
      totalcontractT = (Contract)totalthiscontract.get(0);
    else {
      totalcontractT = new Contract();
    }

    MarketTotal mt = new MarketTotal();
    if ((totalcustomerL.getTotalnewcustomercount() == null) || (totalcustomerL.getTotalnewcustomercount().intValue() == 0)) {
      mt.setTotalAddedCustomerCount(new BigDecimal(0));
    } else if (((totalcustomerL.getTotalnewcustomercount() != null) && (totalcustomerL.getTotalnewcustomercount().intValue() != 0) && (totalcustomerT.getTotalnewcustomercount() == null)) || (totalcustomerT.getTotalnewcustomercount().intValue() == 0)) {
      mt.setTotalAddedCustomerCount(new BigDecimal(-100));
    } else {
      int t = totalcustomerT.getTotalnewcustomercount().intValue();
      int l = totalcustomerL.getTotalnewcustomercount().intValue();
      int c = t - l;
      mt.setTotalAddedCustomerCount(new BigDecimal(c).divide(new BigDecimal(l), 2, 5).multiply(new BigDecimal(100)));
    }
    if ((totalcontractL.getYearnewcustomercount() == null) || (totalcontractL.getYearnewcustomercount().intValue() == 0)) {
      mt.setTotalAddedContractCount(new BigDecimal(0));
    } else if (((totalcontractL.getYearnewcustomercount() != null) && (totalcontractL.getYearnewcustomercount().intValue() != 0) && (totalcontractT.getYearnewcustomercount() == null)) || (totalcontractT.getYearnewcustomercount().intValue() == 0)) {
      mt.setTotalAddedContractCount(new BigDecimal(-100));
    } else {
      int t = totalcontractT.getYearnewcustomercount().intValue();
      int l = totalcontractL.getYearnewcustomercount().intValue();
      int c = t - l;
      mt.setTotalAddedContractCount(new BigDecimal(c).divide(new BigDecimal(l), 2, 5).multiply(new BigDecimal(100)));
    }
    if ((totalcontractL.getYeartotalcontractCost() == null) || (totalcontractL.getYeartotalcontractCost().intValue() == 0)) {
      mt.setTotalAddedContractCost(new BigDecimal(0));
    } else if (((totalcontractL.getYeartotalcontractCost() != null) && (totalcontractL.getYeartotalcontractCost().intValue() != 0) && (totalcontractT.getYeartotalcontractCost() == null)) || (totalcontractT.getYeartotalcontractCost().intValue() == 0)) {
      mt.setTotalAddedContractCost(new BigDecimal(-100));
    } else {
      int t = totalcontractT.getYeartotalcontractCost().intValue();
      int l = totalcontractL.getYeartotalcontractCost().intValue();
      int c = t - l;
      mt.setTotalAddedContractCost(new BigDecimal(c).divide(new BigDecimal(l), 2, 5).multiply(new BigDecimal(100)));
    }
    if ((totalcontractL.getYeartotalbackCost() == null) || (totalcontractL.getYeartotalbackCost().intValue() == 0)) {
      mt.setTotalAddedContractBackCost(new BigDecimal(0));
    } else if (((totalcontractL.getYeartotalbackCost() != null) && (totalcontractL.getYeartotalbackCost().intValue() != 0) && (totalcontractT.getYeartotalbackCost() == null)) || (totalcontractT.getYeartotalbackCost().intValue() == 0)) {
      mt.setTotalAddedContractBackCost(new BigDecimal(-100));
    } else {
      int t = totalcontractT.getYeartotalbackCost().intValue();
      int l = totalcontractL.getYeartotalbackCost().intValue();
      int c = t - l;
      mt.setTotalAddedContractBackCost(new BigDecimal(c).divide(new BigDecimal(l), 2, 5).multiply(new BigDecimal(100)));
    }

    for (int i = 0; i < 12; i++) {
      ReportForMarket2 rfm = new ReportForMarket2();
      rfm.setMonthId(Integer.valueOf(i + 1));
      Customer c = (Customer)lastc.get(i);
      Contract t = (Contract)last.get(i);
      Customer m = (Customer)thisc.get(i);
      Contract a = (Contract)this1.get(i);

      rfm.setLastnewcustomercount(c.getNewcustomercount());
      rfm.setLastmonthnewcontractcount(t.getMonthnewcontractcount());
      rfm.setLastmonthtotalcontractCost(t.getMonthtotalcontractCost());
      rfm.setLastmonthtotalbackCost(t.getMonthtotalbackCost());
      rfm.setThisnewcustomercount(m.getNewcustomercount());
      rfm.setThismonthnewcontractcount(a.getMonthnewcontractcount());
      rfm.setThismonthtotalcontractCost(a.getMonthtotalcontractCost());
      rfm.setThismonthtotalbackCost(a.getMonthtotalbackCost());
      if (c.getNewcustomercount() == null) {
        rfm.setAddedPecentnewcustomercount(new BigDecimal(0));
      } else if ((c.getNewcustomercount() != null) && (m.getNewcustomercount() == null)) {
        rfm.setAddedPecentnewcustomercount(new BigDecimal(-100));
      } else {
        int r = m.getNewcustomercount().intValue();
        int v = c.getNewcustomercount().intValue();
        int b = r - v;
        rfm.setAddedPecentnewcustomercount(new BigDecimal(b).divide(new BigDecimal(v), 2, 5).multiply(new BigDecimal(100)));
      }
      if (t.getMonthnewcontractcount() == null) {
        rfm.setAddedPecentmonthnewcontractcount(new BigDecimal(0));
      } else if ((t.getMonthnewcontractcount() != null) && (a.getMonthnewcontractcount() == null)) {
        rfm.setAddedPecentmonthnewcontractcount(new BigDecimal(-100));
      } else {
        int r = a.getMonthnewcontractcount().intValue();
        int v = t.getMonthnewcontractcount().intValue();
        int b = r - v;
        rfm.setAddedPecentmonthnewcontractcount(new BigDecimal(b).divide(new BigDecimal(v), 2, 5).multiply(new BigDecimal(100)));
      }
      if (t.getMonthtotalcontractCost() == null) {
        rfm.setAddedPecentmonthtotalcontractCost(new BigDecimal(0));
      } else if ((t.getMonthtotalcontractCost() != null) && (a.getMonthtotalcontractCost() == null)) {
        rfm.setAddedPecentmonthtotalcontractCost(new BigDecimal(-100));
      } else {
        int r = a.getMonthtotalcontractCost().intValue();
        int v = t.getMonthtotalcontractCost().intValue();
        int b = r - v;
        rfm.setAddedPecentmonthtotalcontractCost(new BigDecimal(b).divide(new BigDecimal(v), 2, 5).multiply(new BigDecimal(100)));
      }
      if (t.getMonthtotalbackCost() == null) {
        rfm.setAddedPecentmonthtotalbackCost(new BigDecimal(0));
      } else if ((t.getMonthtotalbackCost() != null) && (a.getMonthtotalbackCost() == null)) {
        rfm.setAddedPecentmonthtotalbackCost(new BigDecimal(-100));
      } else {
        int r = a.getMonthtotalbackCost().intValue();
        int v = t.getMonthtotalbackCost().intValue();
        int b = r - v;
        rfm.setAddedPecentmonthtotalbackCost(new BigDecimal(b).divide(new BigDecimal(v), 2, 5).multiply(new BigDecimal(100)));
      }

      All.add(rfm);
    }
    model.put("all", All);
    model.put("totalcustomerL", totalcustomerL);
    model.put("totalcustomerT", totalcustomerT);
    model.put("totalcontractL", totalcontractL);
    model.put("totalcontractT", totalcontractT);
    model.put("mt", mt);
    model.put("thisyear", Integer.valueOf(Integer.parseInt(currentYear)));
    model.put("lastyear", LastYear);
    return new ModelAndView(getMarketExpandsAnalysis1(), model);
  }

  public ModelAndView annualWreathCompares(HttpServletRequest request, HttpServletResponse response, MarketReport marketReport) {
    Contract contract = new Contract();
    Contract contractL = new Contract();
    Customer customer = new Customer();
    Customer customerL = new Customer();
    List All = new ArrayList();
    Map model = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String isViewDeptManageReport = "";
    if (request.getSession().getAttribute("isViewDeptManageReport") != null) {
      isViewDeptManageReport = (String)request.getSession().getAttribute("isViewDeptManageReport");
    }
    String isViewAllManageReport = "";
    if (request.getSession().getAttribute("isViewAllManageReport") != null) {
      isViewAllManageReport = (String)request.getSession().getAttribute("isViewAllManageReport");
    }
    String dateNow = DateUtils.getDateByFormat("yyyy-MM-dd");
    String thisYear = dateNow.substring(0, 4);
    String currentYear = "";
    if ((marketReport.getCurrentYear() == null) || (marketReport.getCurrentYear() == ""))
      currentYear = dateNow.substring(0, 4);
    else {
      currentYear = marketReport.getCurrentYear();
    }
    Integer LastYear = Integer.valueOf(Integer.parseInt(currentYear) - 1);
    contract.setYears(currentYear);
    customer.setYears(currentYear);
    contractL.setYears(LastYear.toString());
    customerL.setYears(LastYear.toString());
    List totalthiscustomer = this.manageReportService.getTotalYearNewCustomerCount(customer, isViewDeptManageReport, isViewAllManageReport, userId);
    Customer totalcustomerT;
    if ((totalthiscustomer.size() > 0) && (totalthiscustomer != null))
      totalcustomerT = (Customer)totalthiscustomer.get(0);
    else {
      totalcustomerT = new Customer();
    }
    List totalthiscontract = this.manageReportService.getTotalYearNewContractCount(contract, isViewDeptManageReport, isViewAllManageReport, userId);
    Contract totalcontractT;
    if ((totalthiscontract.size() > 0) && (totalthiscontract != null))
      totalcontractT = (Contract)totalthiscontract.get(0);
    else {
      totalcontractT = new Contract();
    }
    List thisc = this.manageReportService.getEveryMonthNewCustomerCount(customer, isViewDeptManageReport, isViewAllManageReport, userId);
    List this1 = this.manageReportService.getEveryMonthNewContractCount(contract, isViewDeptManageReport, isViewAllManageReport, userId);
    List lastc = this.manageReportService.getEveryMonthNewCustomerCount(customerL, isViewDeptManageReport, isViewAllManageReport, userId);
    List last = this.manageReportService.getEveryMonthNewContractCount(contractL, isViewDeptManageReport, isViewAllManageReport, userId);
    for (int i = 0; i < 12; i++) {
      ReportForMarket2 rfm = new ReportForMarket2();
      rfm.setMonthId(Integer.valueOf(i + 1));
      Customer m = (Customer)thisc.get(i);
      Contract a = (Contract)this1.get(i);
      Customer ml = (Customer)lastc.get(11);
      Contract al = (Contract)last.get(11);
      rfm.setThisnewcustomercount(m.getNewcustomercount());
      rfm.setThismonthnewcontractcount(a.getMonthnewcontractcount());
      rfm.setThismonthtotalcontractCost(a.getMonthtotalcontractCost());
      rfm.setThismonthtotalbackCost(a.getMonthtotalbackCost());

      if (i == 0) {
        if ((m.getNewcustomercount() == null) || (m.getNewcustomercount().intValue() == 0) || (ml.getNewcustomercount() == null) || (ml.getNewcustomercount().intValue() == 0))
          rfm.setAroundaddedPecentnewcustomercount(new BigDecimal(0));
        else {
          rfm.setAroundaddedPecentnewcustomercount(new BigDecimal(m.getNewcustomercount().intValue()).divide(new BigDecimal(ml.getNewcustomercount().intValue()), 2, 5).multiply(new BigDecimal(100)));
        }
        if ((a.getMonthnewcontractcount() == null) || (a.getMonthnewcontractcount().intValue() == 0) || (al.getMonthnewcontractcount() == null) || (al.getMonthnewcontractcount().intValue() == 0))
          rfm.setAroundaddedPecentmonthnewcontractcount(new BigDecimal(0));
        else {
          rfm.setAroundaddedPecentmonthnewcontractcount(new BigDecimal(a.getMonthnewcontractcount().intValue()).divide(new BigDecimal(al.getMonthnewcontractcount().intValue()), 2, 5).multiply(new BigDecimal(100)));
        }
        if ((a.getMonthtotalcontractCost() == null) || (a.getMonthtotalcontractCost().intValue() == 0) || (al.getMonthtotalcontractCost() == null) || (al.getMonthtotalcontractCost().intValue() == 0))
          rfm.setAroundaddedPecentmonthtotalcontractCost(new BigDecimal(0));
        else {
          rfm.setAroundaddedPecentmonthtotalcontractCost(a.getMonthtotalcontractCost().divide(al.getMonthtotalcontractCost(), 2, 5).multiply(new BigDecimal(100)));
        }
        if ((a.getMonthtotalbackCost() == null) || (a.getMonthtotalbackCost().intValue() == 0) || (al.getMonthtotalbackCost() == null) || (al.getMonthtotalbackCost().intValue() == 0L))
          rfm.setAroundaddedPecentmonthtotalbackCost(new BigDecimal(0));
        else
          rfm.setAroundaddedPecentmonthtotalbackCost(a.getMonthtotalbackCost().divide(al.getMonthtotalbackCost(), 2, 5).multiply(new BigDecimal(100)));
      }
      else {
        Customer lost1m = (Customer)thisc.get(i - 1);
        Contract lost1a = (Contract)this1.get(i - 1);
        if ((m.getNewcustomercount() == null) || (m.getNewcustomercount().intValue() == 0) || (lost1m.getNewcustomercount() == null) || (lost1m.getNewcustomercount().intValue() == 0))
          rfm.setAroundaddedPecentnewcustomercount(new BigDecimal(0));
        else {
          rfm.setAroundaddedPecentnewcustomercount(new BigDecimal(m.getNewcustomercount().intValue()).divide(new BigDecimal(lost1m.getNewcustomercount().intValue()), 2, 5).multiply(new BigDecimal(100)));
        }
        if ((a.getMonthnewcontractcount() == null) || (a.getMonthnewcontractcount().intValue() == 0) || (lost1a.getMonthnewcontractcount() == null) || (lost1a.getMonthnewcontractcount().intValue() == 0))
          rfm.setAroundaddedPecentmonthnewcontractcount(new BigDecimal(0));
        else {
          rfm.setAroundaddedPecentmonthnewcontractcount(new BigDecimal(a.getMonthnewcontractcount().intValue()).divide(new BigDecimal(lost1a.getMonthnewcontractcount().intValue()), 2, 5).multiply(new BigDecimal(100)));
        }
        if ((a.getMonthtotalcontractCost() == null) || (a.getMonthtotalcontractCost().intValue() == 0) || (lost1a.getMonthtotalcontractCost() == null) || (lost1a.getMonthtotalcontractCost().intValue() == 0))
          rfm.setAroundaddedPecentmonthtotalcontractCost(new BigDecimal(0));
        else {
          rfm.setAroundaddedPecentmonthtotalcontractCost(a.getMonthtotalcontractCost().divide(lost1a.getMonthtotalcontractCost(), 2, 5).multiply(new BigDecimal(100)));
        }
        if ((a.getMonthtotalbackCost() == null) || (a.getMonthtotalbackCost().intValue() == 0) || (lost1a.getMonthtotalbackCost() == null) || (lost1a.getMonthtotalbackCost().intValue() == 0L))
          rfm.setAroundaddedPecentmonthtotalbackCost(new BigDecimal(0));
        else {
          rfm.setAroundaddedPecentmonthtotalbackCost(a.getMonthtotalbackCost().divide(lost1a.getMonthtotalbackCost(), 2, 5).multiply(new BigDecimal(100)));
        }
      }
      All.add(rfm);
    }

    Customer ctnull = new Customer();
    List getYears = this.manageReportService.getTotalYearNewCustomerCount(ctnull, isViewDeptManageReport, isViewAllManageReport, null);
    model.put("years", getYears);
    model.put("totalcustomerT", totalcustomerT);
    model.put("totalcontractT", totalcontractT);
    model.put("all", All);
    model.put("currentYear", marketReport.getCurrentYear());
    model.put("thisYear", thisYear);

    return new ModelAndView(getAnnualWreathCompares(), model);
  }
}